let express = require('express');
let router = express.Router();
let shoppingAndStarDao = require("../dao/ShoppingAndStarDao");
let pager = require("../util/PagerHelper")

/* GET users listing. */
router.all('/list.do', function(req, res, next) {
    pager.href ="/api/shoppingcard/list.do";
    let table = "shoppingcard",
        where = "",
        and = ""
    shoppingAndStarDao.findBy(table,where,and).then(data => {
      pager.data = data;
    res.json({
      code:200, //结果编码,200表示成功，404表示未找到
      data:pager
    });
  }).catch(err => next(err));
});
/*
根据id删除一条数据
 */
router.get("/del.do",((req,res,next) =>{
    let table = "shoppingcard"
    if(req.query.id){
        console.log(req.query.id)
        shoppingAndStarDao.deleteById(table, req.query.id).then(data => {
            res.json({
                code:200,
                msg: "删除成功"
            })
        }).catch(err => next(err));
    } else {
        res.json({
            code:404,
            msg: "没有传id值"
        })
    }
}));
/*
根据ids删除多条数据
 */
router.get("/delBatch.do",(req,res,next) =>{
    let idsStr = req.query.ids;
    let table = "shoppingcard"
    console.log(idsStr);
    let ids;
    if (Array.isArray(idsStr)){
        ids = idsStr;
    } else {
        ids = idsStr.split(",");
    }
    shoppingAndStarDao.deleteByIds(table, ids).then(data => res.json({
        code: 200,
        msg: "删除成功"
    })).catch(err => next(err));
});

router.get("/CountAdd.do", (req, res, next) => {
    let columns = "count = count + 1"
    columns += ",subtotal = count * price"
    let id = req.query.id;
    let table = "shoppingcard"
    if(id) {
        console.log(id)
        shoppingAndStarDao.updateCount(table, columns, id).then(data => res.json({
            code: 200,
            msg: "添加数量成功"
        })).catch(err => next(err))
    }else {
        res.json({
            code:404,
            msg: "没有传id值"
        })
    }
});
router.get("/CountMouse.do", (req, res, next) => {
    let columns = "count = count - 1"
    columns += ",subtotal = count * price"
    let id = req.query.id;
    let table = "shoppingcard";
    if(id) {
        console.log(id)
        shoppingAndStarDao.updateCount(table, columns, id).then(data => res.json({
            code: 200,
            msg: "减少数量成功"
        })).catch(err => next(err))
    }else {
        res.json({
            code:404,
            msg: "没有传id值"
        })
    }
});

router.get("/sum.do", (req, res, next) => {
    let idsStr = req.query.ids;
    let table = "shoppingcard";
    console.log(idsStr);
    let ids;
    if (Array.isArray(idsStr)){
        ids = idsStr;
    } else {
        ids = idsStr.split(",");
    }
    shoppingAndStarDao.sum(table,ids).then(data => res.json({
        code: 200,
        msg: "总计",
        data
    })).catch(err => next(err));
});

//获取多条数据
router.get("/getmany.do",(req,res,next) =>{
    let idsStr = req.query.ids;
    let table = "shoppingcard"
    console.log(idsStr);
    let ids;
    if (Array.isArray(idsStr)){
        ids = idsStr;
    } else {
        ids = idsStr.split(",");
    }
    shoppingAndStarDao.selectByIds(table, ids).then(data => res.json({
        code: 200,
        msg:"成功",
        data
    })).catch(err => next(err));
});

module.exports = router;
